package com.kalix.btzjjj.txt.api.model;

import org.json.JSONArray;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
/**
 *@ Author  ZhaoHang
 * @Date  2019/8/28
 **/
public class ReadFileUtil {
    //从指定的位置的txt文件中按行读取并存入JSONArray中
    public static JSONArray ReadFileUtil(){
        File file=new File("D:\\report\\report.txt");
        InputStreamReader isr = null;
        BufferedReader reader=null;
        String temp=null;
        int line=1;
        JSONArray jArray=new JSONArray();
        try{
            if(file.isFile() && file.exists()) {
                isr = new InputStreamReader(new FileInputStream(file), "utf-8");
                reader=new BufferedReader(isr);
                while((temp=reader.readLine())!=null){
                    jArray.put(temp);
                    line++;
                }
                isr.close();
            }else{
                System.out.println("找不到指定的文件");
                jArray.put("找不到指定的文件");
            }
        }
        catch(Exception e){
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        finally{
            if(reader!=null){
                try{
                    reader.close();
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
        return jArray;
    }
}
